// Filename: router.js
define([
  'jquery',
  'underscore',
  'backbone',
  'knockout',
  'views/user/UserView',
  'views/user/AddUserView',
  'views/user/EditView',
  'views/user/RemoveView',
  'views/user/HomeView',
  'views/user/LoginView',
  'views/mon/DsMonView',
  'views/order/OrderView',
], function($, _, Backbone,
                  Knockout,
                  UserView, 
                  AddUserView, 
                  EditView, 
                  RemoveView,
                  HomeView,
                  LoginView,
                  DsMonView,
                  OrderView
            ){
  
 var AppRouter = Backbone.Router.extend({
              routes: {
                  '': 'home',
                  'edit/:id_f' : 'edit_f',
                  'addUser': 'addUserFunction',
                  'remove/:id_f': 'remove',
                  'user': 'showUser',
                  'login': 'loginFunction',//login
                  'dsmon': 'dsmonFunction',// danh sach mon
                  'order': 'orderFunction',//goi mon
                  // Default
                  '*actions': 'defaultAction',
                  
                  },
              home: function(){
                var homeView = new HomeView();
                    homeView.render();
              },
              edit_f: function(id_f){
                  var editView = new EditView({user_id:id_f });
                  this.loadView(editView.render());

              },
              addUserFunction:function(){
                  if (typeof addUserView !== "undefined") {
                    addUserView.render();
                  } else {
                     addUserView = new AddUserView();
                     addUserView.render();
                  }             
              },
              orderFunction:function(){
                  if (typeof orderView !== "undefined") {
                    orderView.render();
                  } else {
                     orderView = new OrderView();
                     orderView.render();
                  }             
              },
              showUser: function(){
                 var userView = new UserView();
                     userView.render();
              },
              loginFunction: function(){
                 var loginView = new LoginView();
                     loginView.render();
              },
              dsmonFunction: function(){
                 var dsMonView = new DsMonView();
                     dsMonView.render();
              },
              remove:function(id_f){
   
                // Call render on the module we loaded in via the dependency array
                 aa=JSON.parse(localStorage.arrUser);
                  for (var i =0; i< aa.length;  i++) {
                    if(aa[i].id==id_f) {                  
                      aa.splice(i,1); //xoa bo 1 phan tu trong mang
                      localStorage.setItem("arrUser", JSON.stringify(aa)); //save lai
                      break;
                    }         
                  }  
                  location.href="#/user";
               },
              defaultAction: function (actions) {
                alert("no actions: "+actions);
              },

          });
          // Instantiate the router
           var app_router = new AppRouter;
               app_router._currentView = null;
           
               app_router.loadView = function(view){
                  if(this.currentView){
                    this.currentView.remove();
                    //this.view = view;
                  }
                  this.currentView = view;
                  $("#contentPage").html(this.currentView);
               };
            // Start Backbone history a necessary step for bookmarkable URL's
            console.log("-->router.js");
            Backbone.history.start();
        //-----------------end router 
  
});
